package top.glike.ssl.auto.domain.other;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.util.Date;

@Data
public class AutoSslDomain {

    /**
     * 访问密钥ID
     */
    private String accessKeyId;
    /**
     * 访问密钥Secret
     */
    private String accessKeySecret;
    /**
     * 地域ID
     */
    private String regionId;
    /**
     * 服务器IP地址
     */
    private String ipAddress;
    /**
     * SSH端口
     */
    private Long sshPort;
    /**
     * 登录用户名
     */
    private String username;
    /**
     * 登录密码
     */
    private String password;
    /**
     * Nginx配置目录
     */
    private String nginxConfigPath;
    /**
     * 证书存储基础路径
     */
    private String certBasePath;
    /**
     * Nginx重载命令
     */
    private String nginxReloadCmd;
    /**
     * 域名
     */
    private String domainName;
    /**
     * 证书ID
     */
    private String certificateId;
    /**
     * 订单ID
     */
    private String orderId;
    /**
     * 证书文件名
     */
    private String certFileName;
    /**
     * 证书本地存储路径
     */
    private String certLocalPath;
    /**
     * 证书在服务器的完整路径
     */
    private String certServerPath;
    /**
     * 是否自动续期:1是,0否
     */
    private Boolean autoRenew;
    /**
     * 续期后是否删去老的证书数据:1是,0否
     */
    private Boolean deleteOldCert;
    /**
     * 到期前多少天续期
     */
    private Long renewBeforeDays;
    /**
     * 状态
     */
    private String status;
    /**
     * 证书规格
     */
    private String productCode;
    /**
     * 用户名称
     */
    private String certUsername;
    /**
     * 用户电话
     */
    private String phone;
    /**
     * 用户邮箱
     */
    private String email;
    /**
     * 验证方式
     */
    private String validateType;
    /**
     * 证书颁发时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    private Date issueTime;
    /**
     * 证书过期时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    private Date expireTime;
    /**
     * 最后续期时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    private Date lastRenewTime;
    /**
     * 最后同步到服务器时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    private Date lastSyncTime;

    /**
     * 是否即将到期
     */
    private boolean nearExpiry;

    /**
     * 距离到期天数
     */
    private long daysToExpire;

    /**
     * 证书pem
     */
    private String certificate;
    /**
     * 证书key
     */
    private String privateKey;
    private String keyFile;
    private String certFile;
    private String serverId;
    private String cloudProviderId;
    /**
     * 主域名信息(唯一)
     */
    private String mainDomain;

    /**
     * 检查证书是否即将到期
     */
    public boolean checkNearExpiry() {
        // 默认还剩2天过期时自动续期
        int checkDays = this.getRenewBeforeDays() != null ? Math.toIntExact(this.getRenewBeforeDays()) : 2;

        if (expireTime == null) {
            return false;
        }

        long currentTime = System.currentTimeMillis();
        long expireTime = this.expireTime.getTime();
        long daysDiff = (expireTime - currentTime) / (24 * 60 * 60 * 1000);

        this.daysToExpire = daysDiff;
        this.nearExpiry = daysDiff <= checkDays && daysDiff >= 0;

        return this.nearExpiry;
    }

    /**
     * 检查证书是否已过期
     */
    public boolean isExpired() {
        if (expireTime == null) {
            return false;
        }
        boolean expired = new Date().after(expireTime);

        // 如果已过期，重新计算过期天数（负数表示已过期多少天）
        if (expired) {
            long currentTime = System.currentTimeMillis();
            long expireTime = this.expireTime.getTime();
            this.daysToExpire = (expireTime - currentTime) / (24 * 60 * 60 * 1000);
        }

        return expired;
    }

    /**
     * 获取过期状态描述
     */
    public String getExpiryStatusDescription() {
        if (expireTime == null) {
            return "未知到期时间";
        }

        // 重新计算天数以确保准确性
        long currentTime = System.currentTimeMillis();
        long expireTime = this.expireTime.getTime();
        long daysDiff = (expireTime - currentTime) / (24 * 60 * 60 * 1000);

        if (daysDiff < 0) {
            return "已过期" + Math.abs(daysDiff) + "天";
        } else {
            return "还剩" + daysDiff + "天";
        }
    }
}

